﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace HRdex_Final.Jobs
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");

        string SessionID()
        {
            return Session["EmployeeID"].ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lblConfirmation.Visible = false;

            string JobID = Request.QueryString["JobID"];

            if (JobID == null)
            {
                txtDescription.Enabled = true;
                txtJobID.Enabled = true;
                txtjobTitle.Enabled = true;
                ddlJobtype.Enabled = true;
                txtSkills.Enabled = true;
                //fuResume1.Enabled = true;
            }
            else
            {
                Select();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string JobID = Request.QueryString["JobID"];

            if (txtJobID.Text != JobID)
            {
                SqlCommand cmdInsert = new SqlCommand("INSERT INTO catJobTitle(Title, Description, Skills, DatePosted, JobType, EmployeesID)" +
                    "VALUES (@title, @desc, @skills, @datePost, @jobType, @empid)", conn);

                SqlDataAdapter daInsert = new SqlDataAdapter(cmdInsert);

                conn.Open();
                cmdInsert.Parameters.Add("@title", SqlDbType.VarChar).Value = txtjobTitle.Text;
                cmdInsert.Parameters.Add("@desc", SqlDbType.VarChar).Value = txtDescription.Text;
                cmdInsert.Parameters.Add("@skills", SqlDbType.VarChar).Value = txtSkills.Text;
                cmdInsert.Parameters.Add("@datePost", SqlDbType.VarChar).Value = DateTime.Now;
                cmdInsert.Parameters.Add("@jobType", SqlDbType.VarChar).Value = ddlJobtype.SelectedValue;
                cmdInsert.Parameters.Add("@empID", SqlDbType.VarChar).Value = SessionID();

                cmdInsert.ExecuteNonQuery();
                conn.Close();
                lblConfirmation.Visible = true;
                lblConfirmation.Text = "You successfully add new Job";
            }
            else
            {
                Edit();
            }

            //txtJobType.Enabled = false;
            ddlJobtype.Enabled = false;
            txtSkills.Enabled = false;
            txtDescription.Enabled = false;
            txtJobID.Enabled = false;
            txtjobTitle.Enabled = false;

        }

        public void Select()
        {
            string JobID = Request.QueryString["JobID"];

            if (txtJobID.Text != JobID)
            {
                SqlCommand cmd = new SqlCommand("SELECT catJobTitle.JobID, catJobTitle.Title, catJobTitle.Description, catJobTitle.Skills, catJobTitle.DatePosted, catJobTitle.JobType, " +
                             " formEmployees.LastName + ', ' + formEmployees.FirstName + ' ' + formEmployees.MiddleName AS Name, catJobTitle.attachment " +
                             " FROM catJobTitle INNER JOIN formEmployees ON catJobTitle.EmployeesID = formEmployees.EmployeeID WHERE (catJobTitle.JobID =" + JobID + ")", conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable("dt");
                da.Fill(dt);
                conn.Open();
                txtJobID.Text = dt.Rows[0].ItemArray[0].ToString();
                txtjobTitle.Text = dt.Rows[0].ItemArray[1].ToString();
                txtDescription.Text = dt.Rows[0].ItemArray[2].ToString();
                txtSkills.Text = dt.Rows[0].ItemArray[3].ToString();
                lblPublishedDate.Text = dt.Rows[0].ItemArray[4].ToString();
                //txtJobType.Text = dt.Rows[0].ItemArray[5].ToString();
                ddlJobtype.SelectedValue = dt.Rows[0].ItemArray[5].ToString();
                lblCreatedby.Text = dt.Rows[0].ItemArray[6].ToString();

                conn.Close();
                //txtJobType.Enabled = false;
                ddlJobtype.Enabled = false;
                txtSkills.Enabled = false;
                txtDescription.Enabled = false;
                txtJobID.Enabled = false;
                txtjobTitle.Enabled = false;
            }

        }

        public void Edit()
        {
            string JobID = Request.QueryString["JobID"];

            SqlCommand cmdUpdate = new SqlCommand("UPDATE [catJobTitle] SET [Title]='" + txtjobTitle.Text + "', [Description]='"+txtDescription.Text+"', [Skills]='"+txtSkills.Text+"', [JobType] = '"+ddlJobtype.SelectedValue+"' WHERE JobID ="+JobID, conn);
            SqlDataAdapter daUpdate = new SqlDataAdapter(cmdUpdate);
            conn.Open();

            cmdUpdate.Parameters.AddWithValue("Title", txtjobTitle.Text);
            cmdUpdate.Parameters.AddWithValue("Description", txtDescription.Text);
            cmdUpdate.Parameters.AddWithValue("Skills", txtSkills.Text);
            cmdUpdate.Parameters.AddWithValue("JobType", ddlJobtype.SelectedValue);

            cmdUpdate.ExecuteNonQuery();

            conn.Close();
            lblConfirmation.Visible = true;
            lblConfirmation.Text = "You successfully edit Job post";
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            txtDescription.Enabled = true;
            txtJobID.Enabled = true;
            txtjobTitle.Enabled = true;
            //txtJobType.Enabled = true;
            ddlJobtype.Enabled = true;
            txtSkills.Enabled = true;
            //fuResume1.Enabled = true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtDescription.Text = null;
            txtJobID.Text = null;
            txtjobTitle = null;
            txtSkills = null;
        }





    }
}